package com.example.shop_2208.fragment

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2208.entity.CategoryEntityResult
import com.example.shop_2208.entity.GoodsEntityResult
import com.example.shop_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CateViewModel:BaseViewModel() {
    //一级二级分类的请求
    val cateSuccess = MutableLiveData<CategoryEntityResult>()
    val cateFail = MutableLiveData<String>()
    fun category(){
        viewModelScope.launch {
            try {
                cateSuccess.value = apiServer.category()
            }catch (e:Exception){
                cateFail.value = e.message
            }
        }
    }
    //三级商品的请求
    val goodsSuccess = MutableLiveData<GoodsEntityResult>()
    val goodsFail = MutableLiveData<String>()

    fun goods(id:Int,page:Int,size:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.value = apiServer.goods(id,page,size)
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}